package com.wzg.demo;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.wzg.update.config.UpdateConfig;
import com.wzg.update.view.NumberProgressBar;
import com.wzg.update.listener.OnButtonClickListener;
import com.wzg.update.listener.OnDownloadListener;
import com.wzg.update.manager.DownloadManager;
import com.wzg.update.utils.ULog;

import java.io.File;

/**
 * 2020/5/4重构
 * wzg
 *
 */
public class MainActivity extends AppCompatActivity
        implements OnDownloadListener,
        View.OnClickListener,
        OnButtonClickListener {

    private Context mContext;
    private NumberProgressBar progressBar;
    private DownloadManager manager;
    private final String url = "http://xxx/example.apk";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setTitle(R.string.app_title);//系统的api
        mContext = this;
        ULog.enable(true);

        progressBar = findViewById(R.id.number_progress_bar);

        setOnClick(R.id.btn_1, R.id.btn_2, R.id.btn_3, R.id.btn_4);
        //删除旧安装包,这是下载到本地文件
//        boolean b = ApkUtil.deleteOldApk(this, getExternalCacheDir().getPath() + "/example.apk");
    }

    private void setOnClick(int... id) {
        for (int value : id) {
            this.findViewById(value).setOnClickListener(this);
        }
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_1:
                one_();
                break;
            case R.id.btn_2:
                two_();
                break;
            case R.id.btn_3:
                three_();
                break;
            case R.id.btn_4:
                if (manager != null) manager.cancel();
                break;
            default:
                break;
        }
    }

    private void one_() {
        new AlertDialog.Builder(this)
                .setTitle(R.string.dialog_title)
                .setMessage(R.string.dialog_msg)
                .setPositiveButton(R.string.dialog_confirm, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        progressBar.setProgress(0);
                        manager = DownloadManager.getInstance(mContext);
                        manager.setApkName("example.apk")
                                .setApkUrl(url)
                                .setSmallIcon(R.mipmap.ic_launcher)
                                .download();
                    }
                })
                .show();
    }

    private void two_() {
        progressBar.setProgress(0);
        manager = DownloadManager.getInstance(mContext);
        manager.setApkName("example.apk")
                .setApkUrl(url)
                .setSmallIcon(R.mipmap.ic_launcher)
                .download();
    }

    private void three_() {
        /*
         * 整个库允许配置的内容
         * 非必选
         */
        UpdateConfig configuration = new UpdateConfig()
                //输出错误日志
                .setEnableLog(true)
                //设置自定义的下载
                //.setHttpManager()
                //下载完成自动跳动安装页面
                .setJumpInstallPage(true)
                //设置对话框背景图片 (图片规范参照demo中的示例图)
                //.setDialogImage(R.drawable.ic_dialog)
                //设置按钮的颜色
                //.setDialogButtonColor(Color.parseColor("#E743DA"))
                //设置对话框强制更新时进度条和文字的颜色
                //.setDialogProgressBarColor(Color.parseColor("#E743DA"))
                //设置按钮的文字颜色
                .setDialogButtonTextColor(Color.WHITE)
                //设置是否显示通知栏进度
                .setShowNotification(true)
                //设置是否提示后台下载toast
                .setShowBgdToast(false)
                //设置强制更新
                .setForcedUpgrade(true)
                //设置对话框按钮的点击监听
                .setButtonClickListener(this)
                //设置下载过程的监听
                .setOnDownloadListener(this);

        manager = DownloadManager.getInstance(this);
        manager.setApkName("example.apk")
                .setApkUrl(url)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setShowNewerToast(true)
                .setConfiguration(configuration)
                .setApkVersionCode(2)
                .setApkVersionName("2.1.8")
                .setApkSize("20.4")
                .setApkDescription(getString(R.string.dialog_msg))
//                .setApkMD5("DC501F04BBAA458C9DC33008EFED5E7F")
                .setDialog(new UpdateDialog(this))
                .download();
    }

    @Override
    public void start() {
        //shouDialog
    }

    @Override
    public void downloading(int max, int progress) {
        int curr = (int) (progress / (double) max * 100.0);
        progressBar.setMax(100);
        progressBar.setProgress(curr);
    }

    @Override
    public void done(File apk) {
        //hideDialog
    }

    @Override
    public void cancel() {
        //hideDialog
    }

    @Override
    public void error(Exception e) {
        //error e.geMessage()
    }

    @Override
    public void onButtonClick(int id) {
        Log.e("TAG", String.valueOf(id));
    }

}
